from fastapi import APIRouter, Depends, Query
from app.core.deps import CurrentUser
from app.schemas.admin.admin_storage_provider_schemas import (
    AdminStorageProviderCreate,
    AdminStorageProviderDetail,
    AdminStorageProviderListFilter,
    AdminStorageProviderResponse,
    AdminStorageProviderUpdate,
)
from app.schemas.common import OrderSchema, PaginationParams, PaginationResult
from app.services.admin.admin_storage_provider_service import StorageProviderService
from app.schemas.response import Response


router = APIRouter(prefix="/storage-provider", tags=["存储供应商管理"])


def get_provider_order_schema(
    order_by: str = Query(
        "",
        description="排序字段，多个字段用逗号分隔，字段名前加-表示降序，如：-created_at,id。可用字段：id, name, status, created_at",
    )
) -> OrderSchema:
    """获取供应商排序字段"""
    return OrderSchema.with_allowed_fields(allowed_fields={"id", "name", "status", "created_at"}, order_by=order_by)


@router.get(
    "/list",
    response_model=Response[PaginationResult[AdminStorageProviderResponse]],
    summary="获取存储供应商列表",
    description="获取所有存储供应商的列表信息",
)
async def get_provider_list(
    current_user: CurrentUser,
    filters: AdminStorageProviderListFilter = Depends(),
    order: OrderSchema = Depends(get_provider_order_schema),
    pagination: PaginationParams = Depends(),
) -> Response[PaginationResult[AdminStorageProviderResponse]]:
    """获取存储供应商列表"""
    result = await StorageProviderService.get_provider_list(filters, order, pagination)
    return Response.success(data=result)


@router.get(
    "/detail",
    response_model=Response[AdminStorageProviderDetail],
    summary="获取存储供应商详情",
    description="根据供应商ID获取存储供应商的详细信息",
)
async def get_provider_detail(
    current_user: CurrentUser,
    provider_id: int = Query(..., description="供应商ID", gt=0),
) -> Response[AdminStorageProviderDetail]:
    """获取存储供应商详情"""
    result = await StorageProviderService.get_provider_detail(provider_id)
    return Response.success(data=result)


@router.post(
    "/create",
    response_model=Response[AdminStorageProviderResponse],
    summary="创建存储供应商",
    description="创建新的存储供应商,支持设置为默认供应商",
)
async def create_provider(
    current_user: CurrentUser,
    request: AdminStorageProviderCreate,
) -> Response[AdminStorageProviderResponse]:
    """创建存储供应商"""
    result = await StorageProviderService.create_provider(request)
    return Response.success(data=result)


@router.post(
    "/update",
    response_model=Response[AdminStorageProviderResponse],
    summary="更新存储供应商",
    description="更新存储供应商信息,包括名称、配置等",
)
async def update_provider(
    current_user: CurrentUser,
    request: AdminStorageProviderUpdate,
) -> Response[AdminStorageProviderResponse]:
    """更新存储供应商"""
    result = await StorageProviderService.update_provider(request)
    return Response.success(data=result)


@router.post(
    "/delete",
    response_model=Response,
    summary="删除存储供应商",
    description="删除指定的存储供应商,如果存在关联文件则无法删除",
)
async def delete_provider(
    current_user: CurrentUser,
    provider_id: int = Query(..., description="供应商ID", gt=0),
) -> Response:
    """删除存储供应商"""
    await StorageProviderService.delete_provider(provider_id)
    return Response.success()


@router.post(
    "/set-default",
    response_model=Response,
    summary="设置默认存储供应商",
    description="设置指定供应商为默认供应商,同时会取消其他供应商的默认状态",
)
async def set_default_provider(
    current_user: CurrentUser,
    provider_id: int = Query(..., description="供应商ID", gt=0),
) -> Response:
    """设置默认存储供应商"""
    await StorageProviderService.set_default_provider(provider_id)
    return Response.success()
